import { addEvaluation, getConfirmOrder, getOrderList } from '../../server/orders'
import { orderStatus } from '../../utils/data'
import { verification } from '../../utils/verification'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderStatus,
        orders: [],
        status: 1,
        page: 1,
        isLast: false,
        isLoading: false,
        show: false,
        content: "",
        radio: '1',
        starValue: 0,
        evaluateOrderId: null,
        error_content: "",
        isContent: false,
        active:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getData()
    },

    /**
     * 查询订单列表
     */
    getData() {
        this.setData({
            isLoading: true
        })

        // 处理查询条件
        const { status, page } = this.data
        const data = {
            status,
            page
        }
        getOrderList(data).then(res => {
            this.setData({
                isLoading: false
            })

            this.setData({
                orders: [...this.data.orders, ...res.data]
            })
            console.log(this.data.orders);
            // 判断是否是最后一页
            if (page == res.meta.pagination.total_pages) {
                this.setData({
                    isLast: true
                })
            }
        })
    },

    /**
     * 跳转到订单详情
     */
    toOrderInfo(event) {
        console.log(event.currentTarget.id);
        const order_id = event.currentTarget.id
        wx.navigateTo({
            url: '/pages/orders_info/orders_info?ordersId=' + order_id,
        })
    },

    /**
     * 切换tab触发的事件
     */
    onChange(event) {
        const { index } = event.detail

        // 通过点击不同的tar, 设置查询条件
        this.setData({ status: index + 1 })
        this.setData({
            orders: [],
            page: 1,
            isLast: false,
        })
        this.getData()
    },

    /**
     * 页面触底事件
     */
    onReachBottom() {
        if (this.data.isLast) return
        // 带着分页信息重新请求数据
        this.setData({
            page: this.data.page + 1
        })
        this.getData()
    },
    confirmOrder(e) {
        console.log(e.target.id);
        wx.showModal({
            title: '提示',
            content: '是否要确认收货',
            success: res => {
                if (res.confirm) {
                    // console.log('用户点击确定')
                    getConfirmOrder(e.target.id).then(() => {
                        this.setData({
                            orders: this.data.orders.filter(item => {
                                return item.id != e.target.id
                            })
                        })
                    })
                }
            }
        })
    },
    evaluateOrder(e) {
        // console.log(e.target.id);
        //存储被点击订单的id
        this.setData({
            evaluateOrder: e.target.id,
            show: true
        })
        // console.log(this.data.evaluateOrder);
    },
    updateContent() {
        //判断评论内容,保证评论内容不为空
        // console.log("1111");
        // console.log(!verification.JudgmentNull(this.data.content));
        if (verification.JudgmentNull(this.data.content)) {
            this.setData({
                error_content: "",
                isContent: true
            })
            // console.log(this.data.error_content);
        } else {
            this.setData({
                isContent: false,
                error_content: "评价内容不能为空"
            })
        }
    },
    onChangeRadio(e) {
        this.setData({
            radio: e.detail,
        });
        //   console.log(this.data.radio);
    },
    onPopupClose() {
        this.setData({
            show: false
        })
    },
    onChangeStar(e) {
        this.setData({
            starValue: e.detail,
        });
        //   console.log(this.data.starValue);
    },
    submitEvaluation() {
        // console.log(this.data.isContent);
        if (this.data.isContent) {
            // console.log(this.data.orders);
            // console.log(this.data.evaluateOrder);
            let ordersList = this.data.orders
            // console.log(ordersList);
            ordersList = ordersList.filter(item => item.id == this.data.evaluateOrder).pop()//从所有订单中获取当前要评价的订单
            // console.log(ordersList);
            for (let i = 0; i < ordersList.orderDetails.data.length; i++) {//对订单的循环获取商品的id
                // console.log(ordersList.orderDetails.data[i].id);
                let data = {
                    goods_id: ordersList.orderDetails.data[i].goods_id,
                    content: this.data.content,
                    rate: this.data.radio,
                    star: this.data.starValue
                }
                // console.log(data);
                addEvaluation(this.data.evaluateOrder, data).then(() => {//调用评价商品的接口
                    this.setData({
                        show: false,
                        content:'',
                        radio:"1",
                        starValue:0
                    })
                })
            }
        }
    }
})